<?php

namespace api\modules\v1\models;

use Yii;
use api\common\models\Common;
use yii\web\HttpException;
use api\plug\alidayu\TopSdk;
class Code extends Common
{
    public static function tableName()
    {
        return '{{%code_app}}';
    }

    /*
     * 保存验证码
     * @param $code
     */
    public function saveCode($phone='',$code=''){
        if($phone == '' || $code == '') throw new HttpException(401,'验证码参数验证失败',0);
        $expiration_time = time() + Yii::$app->params['code_expiration'];
        if($this->findOne(['phone'=>$phone])){
            if(self::updateAll(['code'=>$code,'expiration_time'=>$expiration_time],['phone'=>$phone])){
                return true;
            }
        }else{
            $this->phone = $phone;
            $this->code = $code;
            $this->expiration_time = $expiration_time;
            if($this->save()){
                return true;
            }
        }
        return false;
    }

    /*
     * 验证验证码
     * @param string $phone
     * @param string $code
     * @return bool
     */
    public function validateCode($phone='',$code=''){
        if($phone == '' || $code == '') return ['success'=>false,'msg'=>'参数丢失!'];
        if($data = $this->findOne(['phone'=>$phone])){
            if(time() > $data['expiration_time']){
                return ['success'=>false,'msg'=>'验证码已过期 请重新发送!'];
            }
            if($code != $data['code']){
                return ['success'=>false,'msg'=>'验证码错误!'];
            }
            $this->deleteAll(['phone'=>$phone]);
            return ['success'=>true,'msg'=>''];
        }
        return ['success'=>false,'msg'=>'请先发送验证码!'];
    }


    /*
     * 创建短信验证码
     * @param int $length
     * @return int
     */
    public static function generate_code($length = 6) {
        return rand(pow(10,($length-1)), pow(10,$length)-1);
    }


    /*
     * 生成验证码 发送验证码
     * @return int
     */
    public function makeCode($phone){
        $code = self::generate_code(6);
        if($this->alidayu($phone,$code)){
            return $code;
        }else{
            throw new HttpException(500,'验证码发送失败',0);
        }
    }

    /*
     * 阿里大鱼平台短信接口
     * @param $phone
     * @param $code
     */
    public function alidayu($phone,$code){
        return true;
        require '../plug/alidayu/TopSdk.php';//引入加载相关的类文件
        date_default_timezone_set('Asia/Shanghai');
        $name = \Yii::$app->params['web_name'];
        $c = new \TopClient();
        $c->appkey = Yii::$app->params['dy_appkey'];
        $c->secretKey = Yii::$app->params['dy_secretKey'];
        $c->format = 'json';
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName(Yii::$app->params['dy_free_sign_name']);
        $req->setSmsParam("{\"code\":\"$code\",\"product\":\"$name\"}");
        $req->setRecNum($phone);
        $req->setSmsTemplateCode(Yii::$app->params['dy_template_code']);
        $resp = $c->execute($req);
        //$resp = is_object($resp) ? get_object_vars($resp) : $resp;
        //print_r($resp);exit;
        if(isset($resp->code) && $resp->code !=0){
            throw new HttpException(500,'发送失败,错误代码:'.$resp->code.' 错误信息:'.isset($resp->sub_msg)? $resp->sub_msg :$resp->msg,0);
        }elseif(isset($resp->result->err_code)){
            if($resp->result->err_code == '0'){   //发送成功
                return true;
            }else{
                throw new HttpException(500,'发送失败,错误代码:'.$resp->result->err_code.' 错误信息:'.$resp->result->msg,0);
            }
        }
        return false;
    }

    /*
     * 阿里大鱼平台短信接口
     * @param $phone
     * @param $code
     */
    public static function alidayuReg($phone){
        //return true;
        require '../plug/alidayu/TopSdk.php';//引入加载相关的类文件
        date_default_timezone_set('Asia/Shanghai');
        $name = \Yii::$app->params['web_name'];
        $c = new \TopClient();
        $c->appkey = Yii::$app->params['dy_appkey'];
        $c->secretKey = Yii::$app->params['dy_secretKey'];
        $c->format = 'json';
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName(Yii::$app->params['dy_free_sign_name']);
        $req->setSmsParam("{\"phone\":\"$phone\"}");
        $req->setRecNum($phone);
        $req->setSmsTemplateCode(Yii::$app->params['reg_template_code']);
        $resp = $c->execute($req);
        if(isset($resp->code) && $resp->code !=0){
            throw new HttpException(500,'发送失败,错误代码:'.$resp->code.' 错误信息:'.isset($resp->sub_msg)? $resp->sub_msg :$resp->msg,0);
        }elseif(isset($resp->result->err_code)){
            if($resp->result->err_code == '0'){   //发送成功
                return true;
            }else{
                throw new HttpException(500,'发送失败,错误代码:'.$resp->result->err_code.' 错误信息:'.$resp->result->msg,0);
            }
        }
        return false;
    }

}
